package com.loto.insurance.mis.web.controller;

import java.math.BigDecimal;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.loto.insurance.mis.web.entity.OemInfo;
import com.loto.insurance.mis.web.entity.ajax.BaseJson;
import com.loto.insurance.mis.web.entity.service.Base;
import com.loto.insurance.mis.web.entity.service.OrderAir;
import com.loto.insurance.mis.web.entity.service.OrderTravel;
import com.loto.insurance.mis.web.entity.service.PaidOrder;
import com.loto.insurance.mis.web.entity.service.PaidPolicy;
import com.loto.insurance.mis.web.entity.service.PayInfo;
import com.loto.insurance.mis.web.entity.service.FeeInit;
import com.loto.insurance.mis.web.entity.service.SimpleOrderInfo;
import com.loto.insurance.mis.web.entity.service.SimplePolicyInfo;
import com.loto.insurance.mis.web.enumeration.ControllerPropertyEnum;
import com.loto.insurance.mis.web.enumeration.InsTypeEnum;
import com.loto.insurance.mis.web.service.AuthorityService;
import com.loto.insurance.mis.web.service.OemService;
import com.loto.insurance.mis.web.service.OrderInfoService;
import com.loto.insurance.mis.web.service.PayService;
import com.loto.utils.XML;

@Controller
public class WebserviceController {
  static Logger logger = LogManager.getLogger(WebserviceController.class);

  @Autowired
  OemService oemService;

  @Autowired
  AuthorityService authorityService;

  @Autowired
  OrderInfoService orderInfoService;

  @Autowired
  PayService payService;

  // 创建订单
  @RequestMapping(value = "/webservice/createorder.service", method = RequestMethod.POST)
  public ModelAndView createorderservice(@RequestParam("insType") int insType,
	  @RequestParam("content") String content) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);
	if (insType == InsTypeEnum.AIR.getIndex()) {
	  OrderAir order = XML.fromXML(content, OrderAir.class);
	  if (order != null) {
		long custId = authorityService.getCustFid(order.getId(),
		    order.getAcc(), order.getPin());
		if (custId > 0) {
		  String platformId = orderInfoService.createOrderAir(order, custId);
		  if (platformId != null && !platformId.isEmpty()) {
			baseJson.setOk(true);
			baseJson.setMsg(platformId);
		  }
		} else {
		  baseJson.setMsg(ControllerPropertyEnum.OEM_AUTHORITY_FAILED
			  .toString());
		}
	  }
	} else if (insType == InsTypeEnum.TRAVEL.getIndex()) {
	  OrderTravel order = XML.fromXML(content, OrderTravel.class);
	  if (order != null) {
		long custId = authorityService.getCustFid(order.getId(),
		    order.getAcc(), order.getPin());
		if (custId > 0) {
		  String platformId = orderInfoService.createOrderTravel(order, custId);
		  if (platformId != null && !platformId.isEmpty()) {
			baseJson.setOk(true);
			baseJson.setMsg(platformId);
		  }
		} else {
		  baseJson.setMsg(ControllerPropertyEnum.OEM_AUTHORITY_FAILED
			  .toString());
		}
	  }
	}
	ModelAndView mav = new ModelAndView("result");
	mav.addObject("result", baseJson);
	return mav;
  }

  // 获取订单支付信息
  @RequestMapping(value = "/webservice/getpayinfo.service", method = RequestMethod.POST)
  public ModelAndView getpayinfoservice(@RequestParam("id") String id,
	  @RequestParam("acc") String acc, @RequestParam("pin") String pin) {
	PayInfo payInfo = new PayInfo();

	OemInfo oemInfo = authorityService.getPayInfo(id, acc, pin);
	if (oemInfo != null) {
	  payInfo.setUsefulFee(oemInfo.getCustUsefulFee());
	  if (oemInfo.getOemIsPayeeAlipay() == 1) {
		payInfo.setPayer("OEM");
	  } else {
		payInfo.setPayer("PLATFORM");
		payInfo.setPartner("2088002020363082");
		payInfo.setSecret("arm37z72ggd7nh65mc7plv7ko1qw0avt");
		payInfo.setSellerEmail("icosuyu@sina.com");
	  }
	}
	ModelAndView mav = new ModelAndView("result");
	mav.addObject("result", payInfo);
	return mav;
  }

  // 订单支付
  @RequestMapping(value = "/webservice/orderpay.service", method = RequestMethod.POST)
  public ModelAndView orderpayservice(@RequestParam("content") String content) {
	PaidOrder paidOrder = new PaidOrder();
	paidOrder.setOk(false);

	FeeInit feeInit = XML.fromXML(content, FeeInit.class);
	if (feeInit != null) {
	  long custId = authorityService.getCustFid(feeInit.getId(),
		  feeInit.getAcc(), feeInit.getPin());
	  if (custId > 0) {
		String platformId = payService.doPay(feeInit.getCustFeeInit(), custId);
		if (platformId != null && !platformId.isEmpty()) {
		  // 投保
		  List<PaidPolicy> policyList = payService.doApproval(platformId);
		  if (policyList != null && policyList.size() > 0) {
			paidOrder.setPolicyList(policyList);
			paidOrder.setOk(true);
			paidOrder.setMsg(platformId);
		  } else {
			paidOrder.setOk(false);
			paidOrder
			    .setMsg(ControllerPropertyEnum.ORDER_DATA_WRONG.toString());
		  }
		}
	  } else {
		paidOrder
		    .setMsg(ControllerPropertyEnum.OEM_AUTHORITY_FAILED.toString());
	  }
	}
	ModelAndView mav = new ModelAndView("result");
	mav.addObject("result", paidOrder);
	return mav;
  }

  // 取消订单
  @RequestMapping(value = "/webservice/cancelorder.service", method = RequestMethod.POST)
  public ModelAndView cancelorderservice(@RequestParam("content") String content) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);
	SimpleOrderInfo order = XML.fromXML(content, SimpleOrderInfo.class);
	if (order != null) {
	  long custId = authorityService.getCustFid(order.getId(), order.getAcc(),
		  order.getPin());
	  if (custId > 0) {
		if (orderInfoService.cancelOrder(order, custId)) {
		  baseJson.setOk(true);
		} else {
		  baseJson.setMsg(ControllerPropertyEnum.ORDER_DATA_WRONG.toString());
		}
	  } else {
		baseJson.setMsg(ControllerPropertyEnum.OEM_AUTHORITY_FAILED.toString());
	  }
	}
	ModelAndView mav = new ModelAndView("result");
	mav.addObject("result", baseJson);
	return mav;
  }

  // 退保
  @RequestMapping(value = "/webservice/cancelpolicy.service", method = RequestMethod.POST)
  public ModelAndView cancelpolicyservice(
	  @RequestParam("content") String content) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);
	SimplePolicyInfo policy = XML.fromXML(content, SimplePolicyInfo.class);
	if (policy != null) {
	  long custId = authorityService.getCustFid(policy.getId(),
		  policy.getAcc(), policy.getPin());
	  if (custId > 0) {
		String platId = payService.cancelPolicy(policy, custId);
		if (platId != null) {
		  baseJson.setOk(true);
		  baseJson.setMsg(platId);
		} else {
		  baseJson.setMsg(ControllerPropertyEnum.ORDER_DATA_WRONG.toString());
		}
	  } else {
		baseJson.setMsg(ControllerPropertyEnum.OEM_AUTHORITY_FAILED.toString());
	  }
	}
	ModelAndView mav = new ModelAndView("result");
	mav.addObject("result", baseJson);
	return mav;
  }

  // 保单退款（未投保成功）
  @RequestMapping(value = "/webservice/refundpolicy.service", method = RequestMethod.POST)
  public ModelAndView refundpolicyservice(
	  @RequestParam("content") String content) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);
	SimplePolicyInfo policy = XML.fromXML(content, SimplePolicyInfo.class);
	if (policy != null) {
	  long custId = authorityService.getCustFid(policy.getId(),
		  policy.getAcc(), policy.getPin());
	  if (custId > 0) {
		String platId = payService.refundPolicy(policy, custId);
		if (platId != null) {
		  baseJson.setOk(true);
		  baseJson.setMsg(platId);
		} else {
		  baseJson.setMsg(ControllerPropertyEnum.ORDER_DATA_WRONG.toString());
		}
	  } else {
		baseJson.setMsg(ControllerPropertyEnum.OEM_AUTHORITY_FAILED.toString());
	  }
	}
	ModelAndView mav = new ModelAndView("result");
	mav.addObject("result", baseJson);
	return mav;
  }

  // 再次尝试投保

  // 查询帐户余额
  @RequestMapping(value = "/webservice/queryusefulfee.service", method = RequestMethod.POST)
  public ModelAndView queryusefulfeeservice(
	  @RequestParam("content") String content) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);
	Base custInfo = XML.fromXML(content, Base.class);
	if (custInfo != null) {
	  long custId = authorityService.getCustFid(custInfo.getId(),
		  custInfo.getAcc(), custInfo.getPin());
	  if (custId > 0) {
		BigDecimal usefulfee = payService.getUsefulFee(custId);
		baseJson.setOk(true);
		baseJson.setMsg(String.valueOf(usefulfee));
	  } else {
		baseJson.setMsg(ControllerPropertyEnum.OEM_AUTHORITY_FAILED.toString());
	  }
	}
	ModelAndView mav = new ModelAndView("result");
	mav.addObject("result", baseJson);
	return mav;
  }
}
